import 'package:app_admin/models/login_certificate.dart';
import 'package:app_admin/utils/sp_util.dart';

import 'package:sprintf/sprintf.dart';
import 'package:uuid/uuid.dart';

class DataSp {
  static const _server = "server";
  static const _loginAccount = 'loginAccount';

  static const _loginCertificate = 'loginCertificate';
  static const _ignoreUpdate = 'ignoreUpdate';
  static const _deviceID = 'deviceID';
  static const _language = "language";

  DataSp._();

  static init() async {
    await SpUtil().init();
  }

  static String? get imToken => getLoginCertificate()?.imToken;

  static String? get chatToken => getLoginCertificate()?.chatToken;

  static String? get userID => getLoginCertificate()?.userID;

  // static String getKey(String key) {
  //   return sprintf(key, ["#123321#"]);
  // }

  static LoginCertificate? getLoginCertificate() {
    return SpUtil()
        .getObj(_loginCertificate, (v) => LoginCertificate.fromJson(v.cast()));
  }

  static Future<bool>? removeLoginCertificate() {
    return SpUtil().remove(_loginCertificate);
  }

  static Future<bool>? putIgnoreVersion(String version) {
    return SpUtil().putString(_ignoreUpdate, version);
  }

  static String? getIgnoreVersion() {
    return SpUtil().getString(_ignoreUpdate);
  }

  static Map? getServerConfig() {
    return SpUtil().getObject(_server);
  }

  static Map? getLoginAccount() {
    return SpUtil().getObject(_loginAccount);
  }

  ///获取设备id
  static String getDeviceID() {
    String id = SpUtil().getString(_deviceID) ?? '';
    if (id.isEmpty) {
      id = const Uuid().v4();
      SpUtil().putString(_deviceID, id);
    }
    return id;
  }

  ///获取当前语言
  static int? getLanguage() {
    return SpUtil().getInt(_language);
  }
}
